<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\GoodsType;
use App\Http\Controllers\App\BaseController;
use App\Models\Goods;

class GoodsTypeController extends BaseController
{
    /**
     * 添加分类
     *
     * @return void
     */
    public function create( Request $request )
    {
        $name = $request->input('name');
        $pic  = $request->input('pic', '');
        $sort = (int)$request->input('sort', 1);
        $pid  = $request->input('pid', 0);
        $note = $request->input('note', '');

        $admin_id = session('admin_id', 0);
   
        if ( $pid ) {
            $info = GoodsType::where('status', 1)->find($pid);
            
            if ( !$info ) {
                return self::error('请添加正确的父类分类');
            }
        }
       
        GoodsType::create([
            'name'     => $name,
            'app_pic'  => $pic,
            'sort'     => $sort,
            'note'     => $note,
            'pid'      => $pid,
            'admin_id' => $admin_id,
        ]);

        return self::success('添加成功');

    }

    /**
     * 查询分类
     *
     * @return void
     */
    public function show( Request $request )
    {
        $name = $request->input('name', '');

        $data = GoodsType::type_info($name);

        return $this->json( $data, '查询成功');
    }

    /**
     * 修改分类
     *
     * @param Request $request
     * @return void
     */
    public function edit( Request $request )
    {
        $id       = $request->input('id');
        $name     = $request->input('name');
        $pic      = $request->input('pic');
        $sort     = $request->input('sort');
        $pid      = $request->input('pid', 0);
        $note     = $request->input('note', '');
        $status   = $request->input('status', 1);

        $admin_id = session('admin_id', 0);

        if ( $pid ) {
            $info = GoodsType::where('status', 1)->find($pid);

            if ( !$info ) {
                return self::error('请添加正确的父类分类');
            }
        }

        $info = GoodsType::find($id);

        if ( !$info) {
            return $this->error('请传递正确的分类');
        }
      
        $info->name      =  $name;
        $info->app_pic   =  $pic;
        $info->sort      =  $sort;
        $info->note      =  $note;
        $info->status    =  $status;
        $info->pid       =  $pid;
        $info->admin_id  =  $admin_id;

        $info->save();

        return self::success('修改成功');

    }

    /**
     * 获取顶级分类
     *
     * @return void
     */
    public function topType()
    {
        $data = GoodsType::where('pid', 0)->get();

        return self::json( $data, '查询成功');
    }

    /**
     * 根据分类上下架商品
     * @return [type] [description]
     */
    public function sellGood( Request $request )
    {
        $id       = $request->input('id');
        $is_sell  = $request->input('sell');
        $admin_id = session('admin_id');

        $info = GoodsType::find($id);

         if ( $info['pid'] == 0) {
            $ids = GoodsType::where('pid', $id)->pluck('id');
         } else {
            $ids[] = $id;
            
         }
       
        Goods::whereIn('goods_type_id', $ids)->update([
            'is_sell'    => $is_sell,
            'updated_at' => date('Y-m-d H:i:s', time()),
            'admin_id'   => $admin_id,
        ]);

        return self::success('修改成功');
    }   
}
